﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBLL;
using IDAL;
using Models.DTO;
using Models;

namespace BLL
{
	public class SupplierInfoBLL : ISupplierInfoBLL
    {
        private ISupplierInfoDAL _supplierInfoDAL;

        public SupplierInfoBLL(
            ISupplierInfoDAL supplierInfoDAL
            )
        {
            _supplierInfoDAL = supplierInfoDAL;
        }

        #region 获取供应商表的方法 (GetSupplierInfoes)
        /// <summary>
        /// 获取供应商表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="supplierName"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetSupplierInfoDTO> GetSupplierInfo(int limit, int page, string supplierName, out int count)
        {
            var data = _supplierInfoDAL.GetEntities()
                       .Where(r => r.IsDelete == false)
                       .Select(r => new GetSupplierInfoDTO
                       {
                           Id = r.Id,
                           SupplierName = r.SupplierName,
                           SupplierContact = r.SupplierContact,
                           SupplierPhone = r.SupplierPhone,
                           CreateTime = r.CreateTime,
                           IsDelete = r.IsDelete
                       });

            if (!string.IsNullOrWhiteSpace(supplierName))
            {
                //条件精准查询
                data = data.Where(d => d.SupplierName == supplierName);
            }
            //计算数据总数
            count = data.Count();
            //分页(降序)
            return data.OrderByDescending(it => it.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();
        }
        #endregion

        #region 添加供应商的方法 (CreateSupplierInfo)
        /// <summary>
        /// 添加供应商的方法
        /// </summary>
        /// <param name="cate">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool CreateSupplierInfo(SupplierInfo cate, out string msg)
        {
            if (string.IsNullOrWhiteSpace(cate.SupplierName))
            {
                msg = "供应商名称不能为空";
                return false;
            }
            //验证供应商是否存在
            if (_supplierInfoDAL.GetEntities().FirstOrDefault(it => it.SupplierName == cate.SupplierName) != null)
            {
                msg = "供应商名称已经存在";
                return false;
            }
            cate.Id = Guid.NewGuid().ToString();//用户id
            cate.CreateTime = DateTime.Now;//设置时间
            bool IsSuccess = _supplierInfoDAL.CreateEntity(cate);//调用方法
            msg = IsSuccess ? $"添加{cate.SupplierName}ZAXCFXFFD成功" : "添加用户失败";

            return IsSuccess;
        }
        #endregion

        #region 删除的方法 (DeleteSupplierInfo)
        /// <summary>
        /// 删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteSupplierInfo(string id)
        {
            SupplierInfo cate = _supplierInfoDAL.GetEntityById(id);
            if (cate == null)
            {
                return false;
            }
            cate.IsDelete = true;
            cate.DeleteTime = DateTime.Now;
            _supplierInfoDAL.UpdateEntity(cate);
            return true;
        }
        #endregion

        #region 批量删除的方法 (DeleteListSupplierInfo)
        /// <summary>
        /// 批量删除的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteListSupplierInfo(List<string> ids)
        {
            List<SupplierInfo> cateList = _supplierInfoDAL.GetEntities().Where(it => ids.Contains(it.Id)).ToList();
            foreach (var item in ids)
            {
                SupplierInfo cate = _supplierInfoDAL.GetEntityById(item);
                if (cateList == null)
                {
                    return false;
                }
                cate.IsDelete = true;
                cate.DeleteTime = DateTime.Now;

                _supplierInfoDAL.UpdateEntity(cate);
            }
            return true;
        }
        #endregion

        #region 修改的方法 (UpdateSupplierInfo)
        /// <summary>
        /// 修改用户的方法
        /// </summary>
        /// <param name="dept"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateSupplierInfo(SupplierInfo cate, out string msg)
        {
            if (string.IsNullOrWhiteSpace(cate.SupplierName))
            {
                msg = $"供应商名称不能为空";
                return false;
            }
            SupplierInfo entity = _supplierInfoDAL.GetEntityById(cate.Id);
            if (entity.Id == null)
            {
                msg = "ID不存在";
                return false;
            }
            //判断重复
            if (entity.SupplierName != cate.SupplierName)
            {
                var data = _supplierInfoDAL.GetEntities().FirstOrDefault(it => it.SupplierName == cate.SupplierName);
                if (data != null)
                {
                    msg = "供应商名已经被占用";
                    return false;
                }
            }

            entity.SupplierName = cate.SupplierName;
            entity.SupplierContact = cate.SupplierContact;
            entity.SupplierPhone = cate.SupplierPhone;
            bool IsSuccess = _supplierInfoDAL.UpdateEntity(entity);//调用方法

            msg = IsSuccess ? $"修改成功" : "修改失败";

            return IsSuccess;
        }
        #endregion
    }
}
